package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * PROJECT_NAME: sky-take-out
 * NAME: SetmealServiceImpl
 * USER: zhang
 * DATE: 2023/5/18
 * DESCRIPTION :B端-套餐信息服务层接口实现类
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealDishMapper setmealDishMapper;

    /**
     * 添加套餐
     *
     * @param setmealDTO 用于接受套餐和菜品数据的Bean
     */
    @CacheEvict(cacheNames = "SETMEAL_CACHE",allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSetmeal(SetmealDTO setmealDTO) {
        //将DTO中的数据抽取到Setmeal对象中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //将套餐的状态修改为启用
        setmeal.setStatus(StatusConstant.ENABLE);
        //调用持久层进行套餐信息的添加
        setmealMapper.insertSetmeal(setmeal);

        //获取套餐信息中菜品信息的集合
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        Long setmealId = setmeal.getId();
        setmealDishList.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setmealDishMapper.insertSetmealDish(setmealDishList);
    }

    /**
     * 条件分页查询
     * @param setmealPageQueryDTO 分页参数
     * @return 分页查询的结果
     */
    @Override
    public PageResult selectSetmealByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> pageList =  setmealMapper.selectSetmealByPage(setmealPageQueryDTO);
        PageResult pageResult = PageResult.builder().total(pageList.getTotal()).records(pageList.getResult()).build();
        return pageResult;
    }

    /**
     * 批量删除套餐
     * @param setmealIds 要删除套餐信息的集合
     */
    @CacheEvict(cacheNames = "SETMEAL_CACHE",allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSetmeal(List<Long> setmealIds) {
        //遍历获取到每一个套餐id
        for (Long setmealId : setmealIds) {
            //基于套餐setmealId查询到套餐信息返回
            Setmeal setmeal = setmealMapper.selectSetmealById(setmealId);
            //判断套餐的状态，在售状态不能删除 抛出异常，停售状态可以删除
            if (setmeal.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //调用套餐持久层删除套餐信息
        setmealMapper.deleteSetmealById(setmealIds);
        //调用套餐菜品持久层删除菜品信息
        setmealDishMapper.deleteSetmealDishById(setmealIds);
    }

    /**
     * 基于id查询套餐信息
     * @param id 要查询的套餐id
     * @return 将查询到的套餐信息封装为SetmealVO对象返回
     */
    @Override
    public SetmealVO selectSetmealById(Long id) {
        //调用套餐持久层查询套餐的基本信息
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        //调用套餐菜品关联表查询套餐关联的菜品信息
        List<SetmealDish> setmealDishList = setmealDishMapper.selectsetMealDishById(id);
        //将Setmeal封装为SetmealVO对象返回
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //将查询到的菜品信息赋值给setmealDishes集合
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 套餐的启售/停售
     */
    @CacheEvict(cacheNames = "SETMEAL_CACHE",allEntries = true)
    @Override
    public void modifyStatus(Setmeal setmeal) {
        setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 根据分类id查询套餐信息
     *
     * @param categoryId 类别id
     * @return 套餐List集合
     */
    @Override
    public List<Setmeal> getSetmealByCategoryId(Long categoryId) {
        List<Setmeal> setmealList = setmealMapper.selectSetmealByCategoryId(categoryId);
        return setmealList;
    }

    /**
     * 根据套餐Id查询包含菜品信息
     *
     * @param setmealId 套餐Id
     * @return 全局通用返回信息Bean(套餐中菜品的基本信息)
     */
    @Override
    public List<DishItemVO> selectSetmealDishBySetmealId(Long setmealId) {
        //调用套餐菜品关联持久层查询套餐中菜品的基本信息
        List<DishItemVO> dishItemVOList = setmealDishMapper.selectSetmealDishBySetmealId(setmealId);
        return dishItemVOList;
    }

    /**
     * 修改套餐信息
     * @param setmealDTO 修改后套餐信息
     */
    @CacheEvict(cacheNames = "SETMEAL_CACHE",allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //将SetmealDTO中的数据封装到Setmeal中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //调用持久层进行基本信息的修改
        setmealMapper.updateSetmeal(setmeal);
        //调用套餐菜品表删除菜品的信息
        setmealDishMapper.batchDeleteSetmealDish(setmeal.getId());
        //添加菜品数据带套餐菜品关联表中
        List<SetmealDish> setmealDisheList = setmealDTO.getSetmealDishes();
        //将菜品Id放入到套餐菜品关联表中
        setmealDisheList.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        setmealDishMapper.insertSetmealDish(setmealDisheList);
    }
}
